package com.blb.zycommunity.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.zycommunity.config.AutoLog;
import com.blb.zycommunity.entity.ResponseResult;
import com.blb.zycommunity.entity.SysDictData;
import com.blb.zycommunity.entity.SysDictType;
import com.blb.zycommunity.entity.vo.SysDictDataVo;
import com.blb.zycommunity.entity.vo.SysDictTypeVo;
import com.blb.zycommunity.mapper.SysDictDataMapper;
import com.blb.zycommunity.service.ISysDictDataService;
import com.blb.zycommunity.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 字典数据表 前端控制器
 * </p>
 *
 * @author chenheng
 * @since 2023-11-20
 */
@RestController
public class SysDictDataController {

    @Autowired
    private ISysDictDataService iSysDictDataService;

    @Resource
    private SysDictDataMapper sysDictDataMapper;

    @Autowired
    private ISysDictTypeService iSysDictTypeService;

    /**
     * 根据字典类型查询字典数据
     */
    @PostMapping("dictData-dictType")
    public ResponseResult findDictDataByType(@RequestBody SysDictDataVo sysDictDataVo){
        Page<SysDictData> page = new Page<>(sysDictDataVo.getPageNum(),sysDictDataVo.getPageSize());
        sysDictDataMapper.selectAllByDictType(page,sysDictDataVo.getDictType(),sysDictDataVo.getDictLabel(),
                sysDictDataVo.getStatus());
        return ResponseResult.ok(page);
    }

    /**
     * 新增
     * @return
     */
    @AutoLog(value = 1,title = "字典数据")
    @PostMapping("dictData-add")
    public ResponseResult addDictType(@RequestBody SysDictData sysDictData){
        LocalDateTime now = LocalDateTime.now();
        sysDictData.setCreateTime(now);
        iSysDictDataService.save(sysDictData);

        List<SysDictData> dictData = iSysDictDataService.findDictData(sysDictData.getDictType());
        return ResponseResult.ok(dictData);
    }

    /**
     * 修改
     * @return
     */
    @AutoLog(value = 2,title = "字典数据")
    @PutMapping("dictData-update")
    public ResponseResult updateType(@RequestBody SysDictData sysDictData){
        LocalDateTime now = LocalDateTime.now();
        sysDictData.setUpdateTime(now);
        iSysDictDataService.updateById(sysDictData);

        List<SysDictData> dictData = iSysDictDataService.findDictData(sysDictData.getDictType());
        return ResponseResult.ok(dictData);
    }


    /**
     * 删除
     * @param ids
     * @return
     */
    @AutoLog(value = 3,title = "字典数据")
    @DeleteMapping("/dictData/{ids}")
    public ResponseResult deleteById(@PathVariable Long[] ids){
        for (Long id : ids) {
            iSysDictDataService.removeById(id);
        }
        return ResponseResult.ok("删除成功");
    }

    /**
     * 查询所有字典name
    */
    @GetMapping("dictData-type")
    public ResponseResult findType(){
        List<SysDictType> list = iSysDictTypeService.list();
        return ResponseResult.ok(list);
    }


}
